<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滑块验证码</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/Draggable.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            padding: 20px;
        }

        .verification-container {
            width: 100%;
            max-width: 400px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            padding: 30px;
            text-align: center;
            overflow: hidden;
        }

        h2 {
            color: #333;
            margin-bottom: 20px;
            font-weight: 600;
        }

        .instructions {
            color: #666;
            margin-bottom: 25px;
            font-size: 14px;
        }

        .slider-container {
            position: relative;
            height: 50px;
            background: #f0f3f7;
            border-radius: 25px;
            margin: 25px 0;
            overflow: hidden;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .slider-track {
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 100%;
            border-radius: 25px;
        }

        .slider-bg {
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 0;
            background: linear-gradient(90deg, #6a11cb 0%, #2575fc 100%);
            border-radius: 25px;
            transition: width 0.3s ease;
        }

        .slider-thumb {
            position: absolute;
            left: 0;
            top: 0;
            width: 50px;
            height: 50px;
            background: white;
            border-radius: 50%;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
            cursor: grab;
            z-index: 10;
            display: flex;
            justify-content: center;
            align-items: center;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .slider-thumb:active {
            cursor: grabbing;
            transform: scale(1.05);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        }

        .slider-thumb::after {
            content: "→";
            color: #6a11cb;
            font-size: 18px;
            font-weight: bold;
        }

        .target-area {
            position: absolute;
            right: 10px;
            top: 5px;
            height: 40px;
            width: 40px;
            background: rgba(106, 17, 203, 0.2);
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            opacity: 0;
            transition: opacity 0.3s;
        }

        .target-area.show {
            opacity: 1;
        }

        .target-icon {
            width: 20px;
            height: 20px;
            background: #6a11cb;
            border-radius: 50%;
        }

        .feedback {
            height: 20px;
            margin-top: 15px;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s;
            opacity: 0;
            transform: translateY(10px);
        }

        .feedback.success {
            color: #4CAF50;
            opacity: 1;
            transform: translateY(0);
        }

        .feedback.error {
            color: #F44336;
            opacity: 1;
            transform: translateY(0);
        }

        .reset-btn {
            margin-top: 20px;
            padding: 10px 20px;
            background: #f0f3f7;
            border: none;
            border-radius: 25px;
            color: #666;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            opacity: 0;
            visibility: hidden;
        }

        .reset-btn.show {
            opacity: 1;
            visibility: visible;
        }

        .reset-btn:hover {
            background: #e0e5eb;
        }
    </style>
</head>
<body>
<div class="verification-container">
    <h2>请完成验证</h2>
    <p class="instructions">拖动滑块到右侧目标位置</p>

    <div class="slider-container">
        <div class="slider-track">
            <div class="slider-bg"></div>
            <div class="target-area">
                <div class="target-icon"></div>
            </div>
        </div>
        <div class="slider-thumb" id="sliderThumb"></div>
    </div>

    <div class="feedback" id="feedback"></div>
    <button class="reset-btn" id="resetBtn">重试</button>
</div>

<script>
    // 初始化元素
    const sliderThumb = document.getElementById('sliderThumb');
    const sliderBg = document.querySelector('.slider-bg');
    const targetArea = document.querySelector('.target-area');
    const feedback = document.getElementById('feedback');
    const resetBtn = document.getElementById('resetBtn');
    const sliderContainer = document.querySelector('.slider-container');

    // 设置目标位置 (80%宽度处)
    const targetPosition = sliderContainer.offsetWidth * 0.8 - 25;
    let isVerified = false;

    // 显示目标区域
    gsap.to(targetArea, {
        opacity: 1,
        duration: 0.5,
        delay: 0.5,
        onStart: () => targetArea.classList.add('show')
    });

    // 初始化Draggable
    const draggable = Draggable.create(sliderThumb, {
        type: 'x',
        bounds: sliderContainer,
        inertia: true,
        edgeResistance: 0.8,
        onDrag: function() {
            // 更新背景填充宽度
            const progress = this.x / (sliderContainer.offsetWidth - 50);
            gsap.to(sliderBg, { width: `${progress * 100}%`, duration: 0.1 });

            // 如果已经验证通过，不再处理
            if (isVerified) return;

            // 检查是否接近目标位置
            const distanceToTarget = Math.abs(this.x - targetPosition);
            if (distanceToTarget < 10) {
                sliderThumb.style.backgroundColor = '#6a11cb';
                sliderThumb.style.color = 'white';
                sliderThumb.innerHTML = '✓';
            } else {
                sliderThumb.style.backgroundColor = 'white';
                sliderThumb.style.color = '#6a11cb';
                sliderThumb.innerHTML = '→';
            }
        },
        onDragEnd: function() {
            // 如果已经验证通过，不再处理
            if (isVerified) return;

            // 检查是否到达目标位置 (允许±5px误差)
            if (Math.abs(this.x - targetPosition) < 5) {
                // 验证成功
                isVerified = true;
                gsap.to(this.target, {
                    x: targetPosition,
                    duration: 0.3,
                    ease: 'back.out(1)'
                });

                // 更新UI
                sliderThumb.style.backgroundColor = '#4CAF50';
                sliderThumb.style.color = 'white';
                sliderThumb.innerHTML = '✓';

                // 显示成功反馈
                feedback.textContent = '验证成功！';
                feedback.className = 'feedback success';

                // 显示重置按钮
                gsap.to(resetBtn, {
                    opacity: 1,
                    duration: 0.5,
                    onStart: () => resetBtn.classList.add('show')
                });

                // 庆祝动画
                gsap.to(sliderThumb, {
                    y: -10,
                    duration: 0.2,
                    repeat: 1,
                    yoyo: true,
                    ease: 'power1.inOut'
                });
            } else {
                // 验证失败
                feedback.textContent = '请将滑块拖动到正确位置';
                feedback.className = 'feedback error';

                // 弹性返回动画
                gsap.to(this.target, {
                    x: 0,
                    duration: 0.8,
                    ease: 'elastic.out(1, 0.5)',
                    onStart: () => {
                        gsap.to(sliderBg, { width: '0%', duration: 0.8 });
                    }
                });
            }
        }
    })[0];

    // 重置功能
    resetBtn.addEventListener('click', function() {
        isVerified = false;

        // 重置滑块位置
        gsap.to(sliderThumb, {
            x: 0,
            duration: 0.5,
            ease: 'power2.out',
            onStart: () => {
                gsap.to(sliderBg, { width: '0%', duration: 0.5 });
                sliderThumb.style.backgroundColor = 'white';
                sliderThumb.style.color = '#6a11cb';
                sliderThumb.innerHTML = '→';
                feedback.className = 'feedback';
            }
        });

        // 隐藏重置按钮
        gsap.to(resetBtn, {
            opacity: 0,
            duration: 0.3,
            onComplete: () => resetBtn.classList.remove('show')
        });
    });
</script>
</body>
</html>